package cn.edu.guet.jdbc.day01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public final class JdbcUtils {
	
	private JdbcUtils(){};
	
	private static String url = "";
	private static String user = "";
	private static String password = "";
	
	private static JdbcUtils instance = null;
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static JdbcUtils getInstance(){
		if(instance==null){
			synchronized (JdbcUtils.class) {
				if(instance==null)
					instance = new JdbcUtils();
			}
		}
		return instance;
	}
	
	public Connection getConnection() throws SQLException{
		return DriverManager.getConnection(url,user,password);
	}
	
	
	public void free(ResultSet rs,Statement st,Connection conn){
		try{
			if(rs != null)
				rs.close();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				if(st != null)
					st.close();
			}catch(SQLException e){
				e.printStackTrace();
			}finally{
				try{
					if(conn != null)
						conn.close();
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
		}
	}
	
}
